import { createRouter, createWebHashHistory } from 'vue-router'
import { Modal } from 'ant-design-vue'

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home/index.vue'),
    children: [],
  },
  { path: '/viewnotice', component: () => import('../views/ViewNotice.vue') },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/index.vue'),
  },
  {
    path: '/signin',
    name: 'signin',
    component: () => import('../views/Login/Signin.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

//导航守卫
router.beforeEach((to, from, next) => {
  // 定义一个登录的状态
  let isLogin = localStorage.getItem('isLogin') ? true : false
  if (isLogin) {
    // 已经登录的状态,不需要重复地去登录
    if (to.path == '/login') {
      next('/home')
    } else {
      next()
    }
  } else {
    // 没有登录的状态,只能访问login页面
    if (to.path != '/login' && to.path != '/signin') {
      Modal.warning({
        content: '请先登录,获取权限!',
      })
      // alert('请先登录,获取权限')
      next('/login')
    } else {
      next()
    }
  }
})

export default router
